__author__ = 'boris'


import sys,os
sys.path.append(sys.argv[0][:sys.argv[0].rfind(os.path.join('com','stocklens'))])

import time
from exceptions                                     import KeyboardInterrupt, SystemExit
from com.stocklens.stock.md.mds                     import MDServer
from com.stocklens.stock.ctrade.ctradeApiController import CTradeApiController
from com.stocklens.stock.iTrader.iTradeManager      import ITradeManager
from com.stocklens.stock.common.utils               import Logging, CTPUtils

def signalHandler():
    pass

def stockTradeStart():
    Logging.initLogger('conf/default.logging.cfg')
    CTPUtils.Singleton = CTPUtils()
    CTPUtils.Singleton.start()

    Logging.LOGGER.debug('System Start...')
    Logging.LOGGER.debug('Create cTrade...')
    tradeApiCtl = CTradeApiController('conf/trade.cfg')
    tradeApiCtl.start()
    orderAggregator = tradeApiCtl.getTradeOrderAgg()

    Logging.LOGGER.debug('Create iTrader...')
    iTradeManager = ITradeManager(orderAggregator, 'conf/itrade.cfg')
    iTradeManager.start()

    try:
        while True:
            iTradeManager.display()
            time.sleep(3600)
    except (KeyboardInterrupt, SystemExit):
        iTradeManager._shutDown()
        tradeApiCtl.stop()
        CTPUtils.Singleton.shutDown()

if __name__ == '__main__':
    stockTradeStart()